package ar.uba.fi.tecnicas.vista;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;

import javax.swing.*;

public abstract class PanelOperacion extends JPanel
{
	protected static final long serialVersionUID = 0;
	
	protected PanelControles panelControles;
	
	protected List<JComponent> componentes;
	
	private JButton btnAceptar = new JButton("Aceptar");
	private JButton btnCancelar = new JButton("Cancelar");
	
	public PanelOperacion(PanelControles panelControles)
	{
		super();
		
		this.setSize(100, 500);
		this.setBackground(Color.GRAY);
		
		this.panelControles = panelControles;
		this.componentes = new ArrayList<JComponent>();
		
		this.inicializarComponentes();
		
		this.agregarBotonesDefault();
		
		this.agregarComponentes();
		this.asignarEventos();
	}
	
	private void agregarBotonesDefault() 
	{
		this.componentes.add(this.btnAceptar);
		this.componentes.add(this.btnCancelar);
		
		this.btnCancelar.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent e){ 
					terminarOperacion();
					panelControles.descargarPanel();
				}
			}
		);
		
		this.btnAceptar.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent e) { 
					realizarOperacion();
					terminarOperacion();
					panelControles.descargarPanel();
				}
			}
		);
	}

	private void agregarComponentes() 
	{
		this.setLayout(new GridLayout(this.componentes.size(), 1));
		
		for(JComponent componente : this.componentes)
		{
			this.add(componente);
		}
		
		this.componentes.clear();
	}

	protected abstract void inicializarComponentes();
	
	protected abstract void asignarEventos();
	
	protected abstract void realizarOperacion();
	
	protected abstract void terminarOperacion();
}
